package nuffzone;

import java.util.*;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.User;

public class activityManager {
	private static final activityManager instance = new activityManager();
	private static final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

	
	
		private activityManager(){
		}
		
		public static activityManager getInstance() {
			return instance;
		}
		
		public void createActivity(String name,String description,Date startDate,Date stopDate,String color,User user, String week, String day) {
			
			Entity activity = null;

			activity = new Entity("Activity");
			activity.setProperty("Name", name);
			activity.setProperty("Description", description);
			activity.setProperty("StartDate", startDate);
			activity.setProperty("StopDate", stopDate);
			activity.setProperty("Color", color);
			activity.setProperty("User", user);
			activity.setProperty("Week", week);
			activity.setProperty("Day", day);
				datastore.put(activity);
		}
		public ArrayList<Activity> getAllUserActivities(User user){
			
			ArrayList<Activity> activityList = new ArrayList<Activity>();
			
			Query activitiesQuery = new Query("Activity");
			List<Entity> results = datastore.prepare(activitiesQuery).asList(
				    FetchOptions.Builder.withDefaults());
			for(Entity e : results)
			{
				User Auser = (User) e.getProperty("User");
				if(Auser.equals(user)){
					
				String Aname = (String) e.getProperty("Name");
				String Adescription = (String) e.getProperty("Description");
				Date AstartDate = (Date) e.getProperty("StartDate");
				Date AstopDate = (Date) e.getProperty("StopDate");
				String Acolor = (String) e.getProperty("Color");
				String Aweek = (String) e.getProperty("Week");
				String Aday = (String) e.getProperty("Day");
				Activity a = new Activity(Aname, Adescription, AstartDate, AstopDate, Acolor, Auser, Aweek, Aday);
				
				activityList.add(a);
				
				}
			}
			return activityList;
		}
		public ArrayList<Activity> getAllActivitiesAsList(){
			
			ArrayList<Activity> activityList = new ArrayList<Activity>();
			
			Query activitiesQuery = new Query("Activity");
			List<Entity> results = datastore.prepare(activitiesQuery).asList(
				    FetchOptions.Builder.withDefaults());
			for(Entity e : results)
			{
				String Aname = (String) e.getProperty("Name");
				String Adescription = (String) e.getProperty("Text");
				Date AstartDate = (Date) e.getProperty("StartDate");
				Date AstopDate = (Date) e.getProperty("StopDate");
				String Acolor = (String) e.getProperty("Color");
				User Auser = (User) e.getProperty("User");
				String Aweek = (String) e.getProperty("Week");
				String Aday = (String) e.getProperty("Day");
				Activity a = new Activity(Aname, Adescription, AstartDate, AstopDate, Acolor, Auser, Aweek, Aday);
				
				activityList.add(a);
			}
			return activityList;
		}
		
		
}